Skip to content

Preserve runtime expression ordering for pruned spill syntax - #665

Open
jogibear9988 wants to merge 2 commits into
dadhi:masterfrom
jogibear9988:fix/runtime-expression-structural-spills-20261114
Open

jogibear9988 wants to merge 2 commits into
dadhi:masterfrom
jogibear9988:fix/runtime-expression-structural-spills-20261114

Conversation

@jogibear9988

@jogibear9988 jogibear9988 commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Fix argument/implicit-Compile ordering for runtime Expression<TDelegate> invocations whose arguments contain try/goto syntax in branches FEC discards. The difference is observable with side effects and null/invalid runtime expressions.

Normal collection visits live argument syntax once. It records structural spill information when pruning conditional/switch syntax; nested runtime invocations propagate that information. Emitted exception-region and label metadata remain separate.

Discarded syntax uses an iterative cursor stack with eight inline entries. Child cursors avoid queueing wide sibling lists; unary chains advance in a local loop. The scanner does not reduce extensions, collect bindings, or enter nested lambda/quote data. Live extensions use their existing reduction/collection path. No recursive ExpressionVisitor or second walk of live arguments remains.

Validation

Full Release solution build and Standard/LightExpression native suites pass for net8.0, net10.0 and net472. The original regressions are retained. Added execution/exception-order tests cover 50,000-node discarded chains, 1,000-expression discarded blocks, nested invocations, lambda/quote boundaries, discarded bindings/extensions, switch pruning and initializer syntax.

Measurements

Fresh comparison: master da14580f10fd19ae2361a8e91cfad4b2dfec182a, prior PR head 3078c8b65b0c789f46fc5e80bd78b3f58495c780, revised head e95d0ffcf5bc4b33c5ea419f16d39bbb8532613c. All variants use SDK 10.0.301 and task-local ARM64 runtimes 8.0.28 / 10.0.9 on the same Windows machine.

Both APIs use FEC; “System trees” identifies the expression API, not the System compiler. Trees, conversions and adapters are prepared outside measurement. Compilation includes delegate creation. Runtime expression invocation includes System Compile on every call, without caching.

Two opposite-load-order process runs; seven samples per run with rotated variant order; medians of 14 samples. Calibration targets 150 ms across all three variants. Tiering disabled; process pinned to one logical CPU. Allocations use GC.GetAllocatedBytesForCurrentThread. All observations, including slower results and unchanged-IL controls, are retained.

The visitor allocation is removed. Required spill setup remains: the measured dead-try path is 104 B above master, down from 128 B. Large discarded syntax still requires inspection, so compilation can remain slower than master, which skips that inspection. Timing differences do not support a blanket no-regression claim.

Full compilation and runtime results

Values are master → prior PR → revised PR.

Runtime / API Workload Compile μs Compile B/op Runtime ns Runtime B/op
net10.0 / LightExpression nested_lambda 4.711 → 4.715 → 4.882 2048.0 → 2048.0 → 2048.0 1.807 → 1.816 → 1.837 0.0 → 0.0 → 0.0
net10.0 / LightExpression runtime_expression_dead_switch 5.732 → 6.723 → 6.674 1568.0 → 1824.0 → 1672.0 16062.426 → 16155.592 → 16037.656 4256.0 → 4256.0 → 4256.0
net10.0 / LightExpression runtime_expression_dead_try 5.886 → 6.800 → 6.897 1568.0 → 1696.0 → 1672.0 16159.059 → 16375.034 → 15809.736 4256.1 → 4256.1 → 4256.1
net10.0 / LightExpression runtime_expression_deep_dead_plain 5.623 → 7.837 → 6.223 1568.0 → 1592.0 → 1568.0 16524.627 → 16073.399 → 16647.545 4256.0 → 4256.0 → 4256.0
net10.0 / LightExpression runtime_expression_deep_dead_try 5.598 → 8.554 → 7.031 1568.0 → 1696.0 → 1672.0 16650.987 → 16364.159 → 15956.777 4256.0 → 4256.0 → 4256.0
net10.0 / LightExpression runtime_expression_live_add 5.861 → 5.747 → 5.781 1568.0 → 1592.0 → 1568.0 15960.654 → 16345.605 → 16538.734 4256.0 → 4256.0 → 4256.0
net10.0 / LightExpression runtime_expression_plain 5.633 → 5.590 → 5.690 1568.0 → 1568.0 → 1568.0 16631.597 → 16151.759 → 16404.136 4256.1 → 4256.1 → 4256.1
net10.0 / LightExpression runtime_expression_wide_dead_plain 5.517 → 8.697 → 7.529 1568.0 → 2752.0 → 1568.0 16515.695 → 16837.187 → 16319.619 4256.0 → 4256.0 → 4256.0
net10.0 / LightExpression scalar_add 2.489 → 2.608 → 2.521 872.0 → 872.0 → 872.0 1.833 → 1.859 → 1.881 0.0 → 0.0 → 0.0
net10.0 / System trees nested_lambda 5.394 → 5.396 → 5.384 2048.0 → 2048.0 → 2048.0 1.802 → 1.819 → 1.818 0.0 → 0.0 → 0.0
net10.0 / System trees runtime_expression_dead_switch 6.791 → 8.300 → 8.142 1568.0 → 1760.0 → 1672.0 16807.287 → 17751.387 → 17280.945 4256.0 → 4256.0 → 4256.0
net10.0 / System trees runtime_expression_dead_try 6.790 → 7.963 → 7.891 1568.0 → 1696.0 → 1672.0 16387.386 → 16195.313 → 16350.195 4256.0 → 4256.0 → 4256.0
net10.0 / System trees runtime_expression_deep_dead_plain 6.643 → 10.553 → 7.106 1568.0 → 1592.0 → 1568.0 16890.915 → 16435.099 → 16193.129 4256.0 → 4256.0 → 4256.0
net10.0 / System trees runtime_expression_deep_dead_try 6.690 → 11.663 → 8.210 1568.0 → 1696.0 → 1672.0 16417.397 → 16719.033 → 16540.666 4256.0 → 4256.0 → 4256.0
net10.0 / System trees runtime_expression_live_add 6.899 → 7.086 → 6.961 1568.0 → 1592.0 → 1568.0 16520.487 → 15763.919 → 16092.289 4256.0 → 4256.0 → 4256.0
net10.0 / System trees runtime_expression_plain 6.878 → 6.948 → 6.889 1568.0 → 1568.0 → 1568.0 16674.490 → 16830.710 → 17124.434 4248.0 → 4248.0 → 4248.0
net10.0 / System trees runtime_expression_wide_dead_plain 6.709 → 19.337 → 18.068 1568.0 → 1592.0 → 1568.0 16925.614 → 16666.703 → 16220.016 4256.0 → 4256.0 → 4256.0
net10.0 / System trees scalar_add 3.131 → 3.142 → 3.215 872.0 → 872.0 → 872.0 1.808 → 1.817 → 1.825 0.0 → 0.0 → 0.0
net8.0 / LightExpression nested_lambda 2.330 → 2.307 → 2.335 2048.0 → 2048.0 → 2048.0 1.820 → 1.791 → 1.813 0.0 → 0.0 → 0.0
net8.0 / LightExpression runtime_expression_dead_switch 2.696 → 3.182 → 3.055 1568.0 → 1824.0 → 1672.0 14447.028 → 13813.904 → 14060.962 4256.0 → 4256.0 → 4256.0
net8.0 / LightExpression runtime_expression_dead_try 2.658 → 2.965 → 2.973 1568.0 → 1696.0 → 1672.0 15058.805 → 13872.605 → 15661.005 4256.0 → 4256.0 → 4256.0
net8.0 / LightExpression runtime_expression_deep_dead_plain 2.658 → 4.600 → 3.415 1568.0 → 1592.0 → 1568.0 15212.545 → 14002.486 → 13981.104 4256.0 → 4256.0 → 4256.0
net8.0 / LightExpression runtime_expression_deep_dead_try 2.745 → 4.922 → 3.544 1568.0 → 1696.0 → 1672.0 14534.680 → 14025.663 → 15285.303 4256.0 → 4256.0 → 4256.0
net8.0 / LightExpression runtime_expression_live_add 2.805 → 2.661 → 2.834 1568.0 → 1592.0 → 1568.0 15303.637 → 14082.041 → 15078.654 4256.0 → 4256.0 → 4256.0
net8.0 / LightExpression runtime_expression_plain 2.637 → 2.551 → 2.684 1568.0 → 1568.0 → 1568.0 15349.637 → 17363.542 → 17247.082 4256.0 → 4256.0 → 4256.0
net8.0 / LightExpression runtime_expression_wide_dead_plain 2.733 → 4.561 → 4.622 1568.0 → 2800.0 → 1568.0 15433.753 → 13900.065 → 13676.415 4256.0 → 4256.0 → 4256.0
net8.0 / LightExpression scalar_add 1.134 → 1.066 → 1.109 872.0 → 872.0 → 872.0 1.859 → 1.854 → 1.816 0.0 → 0.0 → 0.0
net8.0 / System trees nested_lambda 2.508 → 2.542 → 2.505 2048.0 → 2048.0 → 2048.0 1.859 → 1.820 → 1.831 0.0 → 0.0 → 0.0
net8.0 / System trees runtime_expression_dead_switch 3.485 → 4.088 → 3.854 1568.0 → 1760.0 → 1672.0 13556.970 → 13057.166 → 13609.131 4256.0 → 4256.0 → 4256.0
net8.0 / System trees runtime_expression_dead_try 3.437 → 3.872 → 3.905 1568.0 → 1696.0 → 1672.0 13077.429 → 13858.545 → 13531.409 4256.0 → 4256.0 → 4256.0
net8.0 / System trees runtime_expression_deep_dead_plain 3.568 → 7.409 → 3.826 1568.0 → 1592.0 → 1568.0 13452.783 → 13384.351 → 13691.089 4256.0 → 4256.0 → 4256.0
net8.0 / System trees runtime_expression_deep_dead_try 3.613 → 7.647 → 4.130 1568.0 → 1696.0 → 1672.0 13948.015 → 13546.260 → 13754.043 4256.0 → 4256.0 → 4256.0
net8.0 / System trees runtime_expression_live_add 3.531 → 3.683 → 3.622 1568.0 → 1592.0 → 1568.0 13503.261 → 13244.584 → 13768.941 4256.0 → 4256.0 → 4256.0
net8.0 / System trees runtime_expression_plain 3.580 → 3.349 → 3.460 1568.0 → 1568.0 → 1568.0 13510.608 → 13176.709 → 13075.061 4248.0 → 4248.0 → 4248.0
net8.0 / System trees runtime_expression_wide_dead_plain 3.399 → 15.280 → 9.065 1568.0 → 1592.0 → 1568.0 14214.783 → 14012.256 → 13796.814 4256.0 → 4256.0 → 4256.0
net8.0 / System trees scalar_add 1.520 → 1.509 → 1.506 872.0 → 872.0 → 872.0 1.829 → 1.826 → 1.830 0.0 → 0.0 → 0.0

Revised and prior PR delegates have identical emitted IL for all measured shared workloads. Runtime allocation changes are reported in the table; no runtime-code speedup is claimed for identical IL.

Known boundary: a separate ordering mismatch involving an immediately invoked lambda body with NoInvocationLambdaInlining also reproduces on master and the prior PR. This change does not add cached lambda-body spill metadata to address that separate case.

Runtime expression invocation must follow System’s structural stack-spill ordering even when FEC prunes a dead try or goto branch. Inspect argument syntax without reducing opaque extension nodes, and defer the implicit Compile call where required.

Tests compare result, exception and side-effect order for valid, null and invalid receivers, including dead conditional branches.
@jogibear9988

Copy link
Copy Markdown
Contributor Author

The current implementation needs reworking before merge. It adds a recursive ExpressionVisitor after normal collection, so selected argument subtrees are walked again, and it introduces another depth-dependent stack risk. FEC already uses loops to avoid recursion along many expression chains.

The underlying ordering bug is real: System considers Try/Goto syntax even in branches FEC prunes, which can change whether argument side effects happen before a null/invalid runtime expression throws during its implicit Compile call. The regression tests should stay.

I am replacing the visitor with structural spill tracking during existing collection, plus an iterative syntax-only scan of discarded branches. Live argument subtrees should not be walked twice; dead syntax must not register bindings, emit exception regions, or reduce extension nodes. I will test both expression APIs and report fresh compilation/runtime timings and allocations against the current patch and master. The previous +128 B result includes spill setup as well as inspection, so removing the visitor alone does not guarantee the entire cost disappears.

Record structural spill information during normal argument collection and inspect only discarded conditional/switch syntax with an iterative cursor stack. Preserve lambda/quote boundaries and keep emitted region metadata separate.

Use child cursors and an inline buffer to avoid wide sibling queues, advance unary chains in place, and bypass the cursor frame for simple discarded nodes. Keep the original ordering regressions and add deep/wide syntax, initializer, boundary and extension coverage.

Validation: full Release build; Standard and LightExpression suites on net8.0, net10.0 and net472; fresh compile/runtime/allocation comparisons against master and the previous PR head. Shared delegates retain identical emitted IL.
@jogibear9988

Copy link
Copy Markdown
Contributor Author

Reworked and pushed in e95d0ff.

The recursive visitor and the second walk of live arguments are gone. Normal collection tracks spill-relevant discarded conditional/switch syntax; an iterative cursor stack inspects only that discarded syntax. Child cursors avoid allocating a queue proportional to sibling count, unary chains advance in place, and simple nodes bypass the cursor-frame method. Bindings and emitted exception/label metadata are not collected from discarded syntax.

The original tests remain. Added execution/order coverage includes 50,000-node discarded chains, 1,000-expression discarded blocks, nested invocations, lambda/quote boundaries, live versus discarded extensions, nested switches and member/list initializers. Full Release build and both expression APIs pass on net8.0, net10.0 and net472.

Fresh comparisons against master and the prior PR head show 24 B less allocation per affected common compilation. The dead-try case drops from 1696 B to 1672 B, versus master's 1568 B: the remaining 104 B is spill setup. The measured wide discarded block saves 1184 B on .NET 10 LightExpression and 1232 B on .NET 8 LightExpression. Emitted IL is identical between prior and revised PR heads for the measured shared cases. Runtime timings/allocations and all slower observations are in the updated description; noisy timings do not justify a blanket speedup claim.

The same visitor exists in #675. I am rebuilding that PR on this commit so dynamic target ordering reuses the collection metadata, with its own before/after measurements.

A separate NoInvocationLambdaInlining ordering case involving an immediately invoked lambda body also reproduces on master and the prior PR. Its reproduction is retained separately; this rework does not add cached lambda-body spill metadata.

@jogibear9988

Copy link
Copy Markdown
Contributor Author

@dadhi you can also look in the first commit, it's much simpler... but has a recursive walk....

@jogibear9988

Copy link
Copy Markdown
Contributor Author

Follow-up on the remaining loop: the discarded-syntax inspection could share the existing collection traversal, but those discarded nodes still need inspecting. Normal collection skips them, so it currently cannot discover their hidden try/goto syntax. The current rework visits live argument syntax once and separately inspects only discarded syntax.

Integrating this into collection would require a syntax-only traversal mode that skips bindings, reductions and other compilation side effects. It could remove the separate walker implementation, but would not eliminate the necessary node visits. The implementation would also need to preserve iterative handling of deep discarded trees and avoid adding overhead to ordinary collection; fewer loops in the source do not by themselves establish a performance improvement.

Doing the detection during IL emission is too late with the current design: FEC needs to decide whether to emit the implicit Compile call (or read a dynamic call-site Target) before emitting the arguments. Discovering a spill afterward would require buffering or rewriting emitted IL.

Sharing the collection machinery is worth evaluating and measuring as a follow-up. The current implementation does not walk the live argument tree twice, but it still has a separate loop over discarded syntax.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant